<script setup>
import { onMounted, ref } from 'vue';
import { useRoute } from 'vue-router';
import { doGet, doPost, doPut } from "../http/httpRequest";

let userInfo = ref({})

let uploadImageRef = ref({})
let uploadContractRef = ref({})

let uploadImageList = ref([])
let uploadContractList = ref([])

let route = useRoute()

// 获取动态路由参数
let id = route.params.id

onMounted(() => {
  // 根据用户id获取用户信息
  doGet("/api/user/" + id, {}).then(function (response) {
    // 处理成功情况
    console.log(response.data);
    if (response.data.code === 200) {
      userInfo.value = response.data.data
    }
  }).catch(function (error) {
    // 处理错误情况
    console.log(error);
  }).finally(function () {
    // 总是会执行
  });
})

function handleImageChange(file, fileList) {
  console.log("文件选择变化,file")
  console.log(file)
  console.log("文件选择变化,fileList")
  console.log(fileList)

  // 文件选择变化时的处理
  uploadImageList.value = fileList;
};

function handleContractChange(file, fileList) {
  // 文件选择变化时的处理
  console.log("文件选择变化, fileList")
  uploadContractList.value = fileList;
};

function submitUpload() {
  if (uploadImageList.value.length == 0 || uploadContractList.value.length == 0) {
    console.log('请先选择文件');
    return;
  }

  console.log("开始上传文件,fileList")
  console.log(uploadImageList)

  const formData = new FormData();
  formData.append('file', uploadImageList.value[0].raw);  // 获取文件对象
  formData.append('id', userInfo.value.id);  // 获取当前用户ID

  doPost("/api/user/image", formData).then(function (response) {
    // 处理成功情况
    console.log("上传图片成功");
    console.log(response.data);
  }).catch(function (error) {
    // 处理错误情况
    console.log(error);
  }).finally(function () {
    // 总是会执行
  });

  //////////////////////////////////////////////////////////////////////////
  const formData2 = new FormData();
  formData2.append('file', uploadContractList.value[0].raw);  // 获取文件对象
  formData2.append('id', userInfo.value.id);  // 获取当前用户ID

  doPost("/api/user/contract", formData2).then(function (response) {
    // 处理成功情况
    console.log("上传合同成功");
    console.log(response.data);
  }).catch(function (error) {
    // 处理错误情况
    console.log(error);
  }).finally(function () {
    // 总是会执行
  });

  // http://localhost:8080/api/user/image
  // uploadImageRef.value.submit()
  // uploadContractRef.value.submit()

  // 更新用户信息
  let formData3 = new FormData();
  for (let field in userInfo.value) {
    //console.log(userInfo.value[field])
    if (field !== 'userImageDO' && field !== 'userContractDO') {
      // 处理null值，若为null则不添加到FormData
      if (userInfo.value[field]) {
        formData3.append(field, userInfo.value[field])
      }
    }
  }
  doPut("/api/user", formData3).then(function (response) {
    // 处理成功情况
    if (response.data.code === 200) {
      console.log("用户信息修改成功");
      console.log(response.data);
    }
  }).catch(function (error) {
    // 处理错误情况
    console.log(error);
  }).finally(function () {
    // 总是会执行
  });
}

function imageSuccess() {
  console.log("头像上传成功")
  uploadImageList.value = []
}
function imageError() {
  console.log("头像上传失败")
  uploadImageList.value = []
}
function contractSuccess() {
  console.log("合同上传成功")
  uploadContractList.value = []
}
function contractError() {
  console.log("头像上传失败")
  uploadContractList.value = []
}
</script>

<template>
  <el-form label-width="auto" :model="userInfo" style="max-width: 600px">
    <el-form-item label="昵称">
      <el-input v-model="userInfo.nick" />
    </el-form-item>
    <el-form-item label="性别">
      <el-input v-model="userInfo.sex" />
    </el-form-item>
    <el-form-item label="手机">
      <el-input v-model="userInfo.phone" />
    </el-form-item>
    <el-form-item label="邮箱">
      <el-input v-model="userInfo.email" />
    </el-form-item>
    <el-form-item label="住址">
      <el-input v-model="userInfo.address" />
    </el-form-item>

    <!-- 上传头像 -->
    <el-form-item label="上传头像">
      <el-upload ref="uploadImageRef" class="avatar-uploader" accept="image/jpg,image/png,image/jpeg"
        :auto-upload="false" action="#" :show-file-list="true" :on-change="handleImageChange"
        :file-list="uploadImageList" :limit="1" :on-success="imageSuccess" :on-error="imageError">
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon">
          <Plus />
        </el-icon>
      </el-upload>
    </el-form-item>

    <!-- 上传合同 -->
    <el-form-item label="上传合同">
      <el-upload ref="uploadContractRef" :auto-upload="false" accept="application/pdf" action="#" :show-file-list="true"
        :on-change="handleContractChange" :file-list="uploadContractList" :limit="1" :on-success="contractSuccess"
        :on-error="contractError">
        <el-button type="primary">Click to upload</el-button>
        <template #tip>
          <div class="el-upload__tip">
            jpg/png files with a size less than 500KB.
          </div>
        </template>
      </el-upload>
    </el-form-item>

    <!-- 提交按钮 -->
    <el-form-item label="提交">
      <el-button type="success" @click="submitUpload">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>